package cn.haohaowo.webservice.client;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import cn.haohaowo.webservice.HelloWorldService;
import cn.haohaowo.webservice.PassPortService;

public class WebServiceFactory {

	public static HelloWorldService getHelloWorldService() {
		Map<String, Object> outProps = new HashMap<String, Object>();
		outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
		outProps.put(WSHandlerConstants.USER, "lwc");
		outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordHandler.class.getName());
		JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
		jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
		jaxWsProxyFactoryBean.getInFaultInterceptors().add(new LoggingOutInterceptor());
		jaxWsProxyFactoryBean.setServiceClass(HelloWorldService.class);
		jaxWsProxyFactoryBean.setAddress("http://localhost:9999/Apache-CXF/webservice/helloWorldService");
		HelloWorldService helloWorldService = (HelloWorldService)jaxWsProxyFactoryBean.create();
		ClientProxy clientProxy = (ClientProxy)Proxy.getInvocationHandler(helloWorldService);
		Client client = clientProxy.getClient();
		client.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));
		
		return helloWorldService;
	}
}
